package com.jy.api.controller.admin;

import com.jy.api.dao.WechatSendTemplateDao;
import com.jy.api.domain.WechatSendTemplate;
import com.jy.api.service.WechatSendTemplateService;
import com.jy.api.signUpReq.admin.SendTemplateSingnUpReq;
import com.jy.api.util.IpUtils;
import com.jy.api.vo.admin.SendTemplateVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * Created by lihu on 2018/1/17.
 */
@Slf4j
@RestController
@RequestMapping(value = "v1/admin/", produces = "application/json;charset=utf-8")
public class WechatSendTemplateContrller {


    @Autowired
    WechatSendTemplateService wechatSendTemplateService;

    @Autowired
    WechatSendTemplateDao wechatSendTemplateDao;


    /**
     * 创建WechatSendTemplate
     *
     * @param singnUpReq
     * @return
     */
    @Transactional
    @RequestMapping(value = "/wechat-send-template", method = RequestMethod.POST)
    public ResponseEntity createSendTemplate(@Valid @RequestBody SendTemplateSingnUpReq singnUpReq,
                                             HttpServletRequest request) {
        try {
            SendTemplateVo sendTemplateVo = wechatSendTemplateService.createTemplate(singnUpReq, IpUtils.getIpAddr(request));
            if (sendTemplateVo == null) {
                return new ResponseEntity("系统繁忙！请稍后重试！", HttpStatus.BAD_REQUEST);
            }
            return new ResponseEntity(sendTemplateVo, HttpStatus.OK);
        } catch (Exception ex) {
            log.error("createSendTemplate is error:{}", ex);
            return new ResponseEntity("系统运行错误", HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }


    /**
     * 获取活动详情
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}/wechat-send-template", method = RequestMethod.GET)
    public ResponseEntity getSendTemplate(@PathVariable String id) {
        try {
            WechatSendTemplate wechatSendTemplate = wechatSendTemplateDao.findOne(id);
            if (wechatSendTemplate == null) {
                return new ResponseEntity("查询失败！信息不存在！", HttpStatus.BAD_REQUEST);
            }
            return new ResponseEntity(wechatSendTemplate, HttpStatus.OK);
        } catch (Exception ex) {
            log.error("createSendTemplate is error:{}", ex);
            return new ResponseEntity("系统运行错误", HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }


}
